<?php
// 斗猿场
namespace plugin\webman\foursport;

use GatewayWorker\Lib\Gateway;
use support\Redis;
use support\Db;
use Workerman\Timer;
use plugin\webman\common\Common;
use plugin\webman\foursport\Sports;
use plugin\webman\gateway\Option;
use plugin\webman\gateway\User;
class Event_four
{
    public static function onWorkerStart($worker)
    {

        if($worker->id === 0){
            $key = 'timer_sports';
            $timer = Redis::get($key);
            var_dump('四道运动会定时器',$timer);
            if($timer==0){
                Timer::add(1, function(){
                    Sports::timer();   //定时发送各个时间阶段的通知
                });
                Redis::set($key, 1); // echo('定时器启动');
            }
        }

    }

    public static function onConnect($client_id)
    {

    }

    public static function onWebSocketConnect($client_id, $data)
    {

    }

    public static function onMessage($client_id, $message)
    {
        var_dump("运动会");
        $data=is_array($message) ? $message : json_decode($message,1);
        $action= $data['action'] ? $data['action'] :'other';
       
        switch ($action) {
            case 'checkurl':
                $checkurl = Sports::checkurl($client_id,$data);
                Gateway::sendToClient($client_id,Common::returndata('returncheckurl',$checkurl));
                break;
            case 'getconfig':
                $config = Option::getconfig();
                Gateway::sendToClient($client_id,Common::returndata('returnconfig',$config));
                break;
             //注册 
             case 'userreg':
                $reg = User::userreg($data['data']);
                
                Gateway::sendToClient($client_id,Common::returndata('returnuserreg',$reg));
                break;
            // 登录
            case 'login':
                $res = User::userlogin($data['data']);
                if($res['code']==1){
                    $key = 'binduid_'.$client_id;  //绑定uid写入redis
                    $useredis = Redis::set($key,$res['info']['userinfo']['id']);
                    Gateway::bindUid($client_id,$res['info']['userinfo']['id']);
                }
                
                Gateway::sendToClient($client_id,Common::returndata('returnlogin',$res));
                break;
            case 'getchargerules':
                $res = User::getchargerules($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('chargerules',$res));
                break;
            case 'charge':
                // 充值
                $res = User::charge($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('payinfo',$res));
                break;
            case 'getdrawrule':
                // 获取提现规则
                $res = User::drawrules($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('drawrules',$res));
                break;
            case 'withdraw':
                // 提现
                $res = User::withdraw($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('withdrawinfo',$res));
                break;  
            case 'enterroom':
                $res = Sports::enterroom($client_id,$data['data']);             
                Gateway::sendToClient($client_id,Common::returndata('returnenterroom',$res));
                break;  
            case 'touzhu':
                $res = Sports::touzhu($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returntouzhu',$res));
                break;
            // case 'tt':
            //     $res = Sports::fenrun();
            //     Gateway::sendToClient($client_id,Common::returndata('returntouzhu',$res));
            //     break;
            case 'myrecord':
                $res = Sports::myrecord($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returnmyrecord',$res));
                break;
            case 'getuserinfo':
                $res = Sports::getuserinfo($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returnuserinfo',$res));
                break;
            
            case 'getresult':
                $res = Sports::getresult($client_id,$data['data']);
                Gateway::sendToClient($client_id,Common::returndata('returngetresult',$res));
                break;
            case 'robotbet':
                $res = Sports::robotbet($client_id,$data['data']);

                break;
            case 'fenrun':
                $res = Sports::fenrun($client_id,$data['data']);

                break;
        }
    }

    public static function onClose($client_id)
    {

    }

}
